package com.cqq.auth.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cqq.auth.constant.AuthMessage;
import com.cqq.auth.dao.PermissionDao;
import com.cqq.auth.dao.domain.Permission;
import com.cqq.auth.service.PermissionService;
import com.cqq.auth.service.bean.PermissionBean;
import com.cqq.auth.service.exception.PermissionServiceException;
import com.cqq.common.core.base.BaseService;
import com.cqq.common.core.constant.CommonMessage;
import com.cqq.common.core.util.BeanUtil;

@Service("permissionService")
public class PermissionServiceImpl extends BaseService implements PermissionService {

	private static final Logger log = LoggerFactory.getLogger(PermissionServiceImpl.class);
	
	@Autowired
	private PermissionDao permissionDao;
	
	
	public void addPermission(PermissionBean perm)
			throws PermissionServiceException {
		log.debug("addPermission ...");
		if(perm==null || StringUtils.isBlank(perm.getSystemCode())
				|| StringUtils.isBlank(perm.getCode())
				|| StringUtils.isBlank(perm.getName())
				|| perm.getType()==null){
			throw super.buildException(new PermissionServiceException(CommonMessage.COMMON_PARAM_EMPTY));
		}
		
		Permission p=this.permissionDao.queryByCode(perm.getCode(), perm.getSystemCode());
		if(p!=null){
			throw super.buildException(new PermissionServiceException(AuthMessage.PERM_CODE_DUPLICATE));
		}
		p=this.permissionDao.queryByName(perm.getName(), perm.getSystemCode());
		if(p!=null){
			throw super.buildException(new PermissionServiceException(AuthMessage.PERM_NAME_DUPLICATE));
		}
		p=new Permission();
		BeanUtil.copy(perm, p);
		this.permissionDao.add(p);
		perm.setId(p.getId());
		log.debug("addPermission ok! {}",perm);
	}

	
	public void updatePermission(PermissionBean perm)
			throws PermissionServiceException {
		log.debug("updatePermission ...");
		if(perm==null || perm.getId()==null 
				|| StringUtils.isBlank(perm.getSystemCode())
				|| StringUtils.isBlank(perm.getCode())
				|| StringUtils.isBlank(perm.getName())
				|| perm.getType()==null){
			throw super.buildException(new PermissionServiceException(CommonMessage.COMMON_PARAM_EMPTY));
		}
		
		Permission p=this.permissionDao.queryByCode(perm.getCode(), perm.getSystemCode());
		if(p!=null && !p.getId().equals(perm.getId())){
			throw super.buildException(new PermissionServiceException(AuthMessage.PERM_CODE_DUPLICATE));
		}
		p=this.permissionDao.queryByName(perm.getName(), perm.getSystemCode());
		if(p!=null && !p.getId().equals(perm.getId())){
			throw super.buildException(new PermissionServiceException(AuthMessage.PERM_NAME_DUPLICATE));
		}
		p=new Permission();
		BeanUtil.copy(perm, p);
		if(this.permissionDao.update(p)!=1){
			throw super.buildException(new PermissionServiceException(CommonMessage.COMMON_UPDATE_ERROR));
		}
		log.debug("updatePermission ok! {}",perm);
	}

	
	public void deletePermission(Integer id) throws PermissionServiceException {
		log.debug("deletePermission ...");
		Permission p=this.permissionDao.queryById(id);
		if(p==null){
			throw super.buildException(new PermissionServiceException(AuthMessage.PERM_NOT_EXIST));
		}
		if(this.permissionDao.delete(id)!=1){
			throw super.buildException(new PermissionServiceException(CommonMessage.COMMON_DELETE_ERROR));
		}
		log.debug("deletePermission ok! id:{}",id);
	}

	
	public List<PermissionBean> listAllPermission(String systemCode) {
		if(StringUtils.isBlank(systemCode)){
			return null;
		}
		List<Permission> perms=this.permissionDao.queryAllBySystem(systemCode);
		List<PermissionBean> beans=BeanUtil.copyList(perms, PermissionBean.class);
		return beans;
	}

	
	public List<PermissionBean> listPermissionByRoleId(Integer id) {
		if(id==null){
			return null;
		}
		List<Permission> perms=this.permissionDao.queryByRoleId(id);
		List<PermissionBean> beans=BeanUtil.copyList(perms, PermissionBean.class);
		return beans;
	}

	
	public List<PermissionBean> listPermissionByUsername(String username,
			String systemCode) {
		if(StringUtils.isBlank(username) || StringUtils.isBlank(systemCode)){
			return null;
		}
		List<Permission> perms=this.permissionDao.queryByUsername(username, systemCode);
		List<PermissionBean> beans=BeanUtil.copyList(perms, PermissionBean.class);
		return beans;
	}

	
	public List<PermissionBean> listPermissionByParentId(Integer parentId,
			String systemCode) {
		if(parentId==null || StringUtils.isBlank(systemCode)){
			return null;
		}
		List<Permission> perms=this.permissionDao.queryByParentId(parentId, systemCode);
		List<PermissionBean> beans=BeanUtil.copyList(perms, PermissionBean.class);
		return beans;

	}

}
